package com.ruoyi.market.service.sms;

import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.block.db.entity.SmsConfigInfo;
import com.ruoyi.market.model.result.SmsInfoResult;
import com.ruoyi.market.service.ISmsInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
public class MiaoXinSmsInfoService implements ISmsInfoService {


    public static void main(String[] args) {
        long timestamp = System.currentTimeMillis();
        // 将时间戳转换为日期对象
        Date date = new Date(timestamp);
        // 按指定格式格式化日期对象
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
        String ts = dateFormat.format(date);
        System.out.println(ts);
    }

    @Override
    public SmsInfoResult getSmsInfo(SmsConfigInfo smsConfigInfo) {

        SmsInfoResult smsInfoResult = new SmsInfoResult();
        smsInfoResult.setPlatformName("秒信");
        smsInfoResult.setSuccess(false);
        try {
            HttpRequest httpRequest = HttpUtil.createPost(smsConfigInfo.getQueryUrl());


            String account = smsConfigInfo.getAccount();
            //获取时间戳并且转为str
            long timestamp = System.currentTimeMillis();
            // 将时间戳转换为日期对象
            Date date = new Date(timestamp);
            // 按指定格式格式化日期对象
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String ts = dateFormat.format(date);

            //格式化raw
            String rawString = StrUtil.format("account={}&ts={}&secret={}", account, ts, smsConfigInfo.getSecret());
            String token = SecureUtil.sha1(rawString);

            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("account", account);
            paramMap.put("token", token);
            paramMap.put("ts", ts);

            httpRequest.form(paramMap);

            String bodyResult = httpRequest.execute().body();
            log.info("【秒信短信服务】【余额查询】发送结束【requesturl】{}【paramMap】{}【bodyResult】{}",
                    smsConfigInfo.getQueryUrl(), JSON.toJSONString(paramMap), bodyResult);

            JSONObject resultJson = JSON.parseObject(bodyResult);
            Integer code = resultJson.getInteger("code");
            String msg = resultJson.getString("msg");

            smsInfoResult.setCode(String.valueOf(code));
            smsInfoResult.setMessage(msg);

            if (code >= 0) {
//                Integer money = resultJson.getInteger("money");
                String RMB = resultJson.getString("RMB");
                smsInfoResult.setBalance(RMB);
                smsInfoResult.setSuccess(true);
            }else{
                smsInfoResult.setSuccess(false);
            }
            return smsInfoResult;
        } catch (Exception exception) {
            exception.printStackTrace();
            smsInfoResult.setCode("9999");
            smsInfoResult.setMessage(exception.getMessage());
        }
        return smsInfoResult;
    }


}
